﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DataControl;
using System.Data;
using System.Text;
using BUS;

namespace ATW.MT.BUS
{
    public class bus_T_Supply
    {
        DataAccess da = null;
        public bus_T_Supply()
        {
            da = new DataAccess();
        }
        #region 方法
        /// <summary>
        /// 供货商分页查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public DataTable SupplyPagerQuery(string name, string status, int page, int pagesize)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select top 100 percent au.tauLoginId,
	                                   au.tauName,
	                                   isnull(au.tauAdditional9,'') companyName,
	                                   isnull(au.tauAdditional7,'0') BidMoney,
	                                   isnull(au.tauAdditional6,'0') [level],
	                                   isnull(au.tauAdditional2,'') code,
                                       au.CreatedDate,
                                       case au.tauRowStatus when '1' then '0' else au.tauRowStatus end status,
	                                   isnull(s1.num,'0') num from T_AppUser au
                                left join 
                                (select tauloginid,COUNT(*) num from T_Response where rowstatus<>'0' and rowstatus<>'2' 
	                                group by tauloginid )s1 on au.tauLoginId=s1.tauloginid
                                where au.tauType='3'");
            if (name != "")
            {
                sql.Append(" and au.tauName='" + name + "'");
            }
            if (status != "")
            {
                sql.Append(" and au.tauRowStatus='" + status + "'");
            }
            return da.PageSize(sql.ToString(), page, pagesize, "tauLoginId", "CreatedDate desc");
        }
        /// <summary>
        /// 供货商查询
        /// </summary>
        /// <param name="tauLoginID"></param>
        /// <returns></returns>
        public DataTable SupplySingleQuery(string tauLoginID)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select au.tauLoginId,
	                                   au.tauName,
	                                   isnull(au.tauAdditional9,'') companyName,
	                                   isnull(au.tauAdditional7,'0') BidMoney,
	                                   isnull(au.tauAdditional6,'0') [level],
	                                   isnull(au.tauAdditional2,'') code,
                                       au.CreatedDate from T_AppUser au where tauLoginId='{0}' 
                                                and au.tauType='3'", tauLoginID);
            return da.ExecuteDataTable(sql.ToString());
        }
        /// <summary>
        /// 获取新邀请码
        /// </summary>
        /// <returns></returns>
        public string getSupplyCode()
        {
            CommonHelp ch = new CommonHelp();
            string code = ch.GetInviteCode("A");
            addSupplyCode(code);
            return code;
        }
        /// <summary>
        /// 新增验证码表
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public int addSupplyCode(string code)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"insert into T_SupplyCode (Code) values('{0}')", code);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 验证码查询
        /// </summary>
        /// <param name="name">所属人</param>
        /// <param name="isUsed">是否使用</param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public DataTable SupplyCodeQuery(string name, string isUsed, int page, int pagesize)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select SC.SCID,
	                                   SC.Code,
	                                   SC.IsUsed,
	                                   isnull(SC.remark,'无') remark,
                                       convert(nvarchar(20),SC.create_time,111) time,
	                                   SC.create_time,
	                                   case when SC.IsUsed='1' then au.tauName	
			                                when SC.IsUsed='0' then '未使用' end name from T_SupplyCode SC
                                left join T_AppUser au on au.tauAdditional2=SC.Code where 1=1");
            if (!String.IsNullOrEmpty(name))
            {
                sql.Append(" and au.tauName like '%" + name + "%'");
            }
            if (!String.IsNullOrEmpty(isUsed))
            {
                sql.Append(" and sc.isused='" + isUsed + "'");
            }
            return da.PageSize(sql.ToString(), page, pagesize, "SCID", "create_time desc");
        }
        /// <summary>
        /// 获取验证码备注
        /// </summary>
        /// <param name="scid"></param>
        /// <returns></returns>
        public string CodeRemarkQuery(string scid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select remark from T_SupplyCode where SCID='{0}'", scid);
            object o = da.ExecuteScalar(sql.ToString());
            if (o != null && o.ToString() != "")
            {
                return o.ToString();
            }
            return "";
        }
        /// <summary>
        /// 新增备注
        /// </summary>
        /// <param name="scid"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        public int CodeRemarkAdd(string scid, string remark)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"update T_SupplyCode set remark='{0}' where SCID='{1}'", remark, scid);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 修改供应商
        /// </summary>
        /// <param name="name"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        public int SupplyEdit(string name, string companyName, string level, string tauloginID)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"update T_AppUser set tauName='{0}',tauAdditional9='{1}',tauAdditional6='{2}' where tauLoginId='{3}'",
                name, companyName, level, tauloginID);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="tauLoginID"></param>
        /// <returns></returns>
        public int SupplyDel(string tauLoginID)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"update T_AppUser set tauRowStatus='2' where tauLoginID='{0}'", tauLoginID);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 恢复
        /// </summary>
        /// <param name="tauLoginID"></param>
        /// <returns></returns>
        public int SupplyRecover(string tauLoginID)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"update T_AppUser set tauRowStatus='0' where tauLoginID='{0}'", tauLoginID);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 查询用户产品
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public DataTable getSupplyProduction(string uid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select * from T_Appuser_Production where tauloginID='{0}'", uid);
            return da.ExecuteDataTable(sql.ToString());
        }
        /// <summary>
        /// 新增用户产品
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="proid"></param>
        /// <returns></returns>
        public string addSupplyProduction(string uid, string proid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"insert into T_Appuser_Production(tauloginID,ProductionID) values('{0}','{1}')", uid, proid);
            return sql.ToString();
        }
        public bool isProductionExist(string pid, string uid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select * from T_Appuser_Production where tauloginID='{0}' and ProductionID='{1}'", uid, pid);
            DataTable dt = da.ExecuteDataTable(sql.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        public int addProduction(string uid, string pid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"insert into T_Appuser_Production (tauloginID,ProductionID)values('{0}','{1}')", uid, pid);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 删除用户产品
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public string removeSupplyProduction(string uid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("delete from T_Appuser_Production where tauloginID='{0}'", uid);
            return sql.ToString();
        }
        /// <summary>
        /// 获取提供材质
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public DataTable getSupplyMaterial(string uid, string pid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select am.MID,
	                                   m.Material_code code_d,
	                                   m.Material_code_z code_z
                                 from T_Appuser_Material am left join C_Material m on am.MID=m.MID
                                 where am.tauloginID='{0}' and am.ProductionID='{1}'", uid, pid);
            return da.ExecuteDataTable(sql.ToString());
        }
        /// <summary>
        /// 增加产品材质
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="mid"></param>
        /// <returns></returns>
        public string addSupplyMaterial(string uid, string mid, string pid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"insert into T_Appuser_Material (tauloginid,mid,type,ProductionID)values('{0}','{1}','4','{2}')", uid, mid, pid);
            return sql.ToString();
        }
        /// <summary>
        /// 移除产品材质
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public string removeSupplyMaterial(string uid, string pid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"delete from T_Appuser_Material where tauloginID='{0}' and ProductionID='{1}'", uid, pid);
            return sql.ToString();
        }
        /// <summary>
        /// 获取产品尺寸
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public DataTable getSupplySize(string uid, string pid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select * from T_Appuser_Size where tauloginID='{0}' and ProductionID='{1}'", uid, pid);
            return da.ExecuteDataTable(sql.ToString());
        }
        /// <summary>
        /// 新增产品尺寸
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="sid"></param>
        /// <returns></returns>
        public string addSupplySize(string uid, string sid, string pid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("insert into T_Appuser_Size(tauloginID,SID,ProductionID)values('{0}','{1}','{2}')", uid, sid, pid);
            return sql.ToString();
        }
        /// <summary>
        /// 移除产品尺寸
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="sid"></param>
        /// <returns></returns>
        public string removeSupplySize(string uid, string pid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"delete from T_Appuser_Size where tauloginID='{0}' and productionID='{1}'", uid, pid);
            return sql.ToString();
        }
        /// <summary>
        /// 获取压力列表
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public DataTable getSupplyPressure(string uid, string pid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select * from T_Appuser_Pressure where tauloginID='{0}' and ProductionID='{1}'", uid, pid);
            return da.ExecuteDataTable(sql.ToString());
        }
        /// <summary>
        /// 新增压力等级
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="PressureID"></param>
        /// <returns></returns>
        public string addSupplyPressure(string uid, string PressureID, string pid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"insert into T_Appuser_Pressure(tauloginid,PressureID,ProductionID)values('{0}','{1}','{2}')",
                uid, PressureID, pid);
            return sql.ToString();
        }
        /// <summary>
        /// 移除压力等级
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public string removeSupplyPressure(string uid, string pid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"delete from T_Appuser_Pressure where tauloginid='{0}' and ProductionID='{1}'", uid, pid);
            return sql.ToString();
        }
        /// <summary>
        /// 获取密封等级
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public DataTable getSupplySeal(string uid, string pid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select * from T_Appsuer_Seal where tauloginID='{0}' and ProductionID='{1}'", uid, pid);
            return da.ExecuteDataTable(sql.ToString());
        }
        /// <summary>
        /// 新增密封等级
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="sealID"></param>
        /// <returns></returns>
        public string addSupplySeal(string uid, string sealID, string pid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"insert into T_Appsuer_Seal(tauloginid,SealID,ProductionID)values('{0}','{1}','{2}')", uid, sealID, pid);
            return sql.ToString();
        }
        /// <summary>
        /// 移除密封等级
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public string removeSupplySeal(string uid, string pid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"delete from T_Appsuer_Seal where tauloginID='{0}' and ProductionID='{1}'", uid, pid);
            return sql.ToString();
        }
        public DataTable getSupplyTemp(string uid, string pid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select * from T_Appuser_Temperature where tauloginID	='{0}' and ProductionID='{1}'", uid, pid);
            return da.ExecuteDataTable(sql.ToString());
        }
        public string addSuppluTemp(string uid, string tempid, string pid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"insert into  T_Appuser_Temperature(tauloginID,up,down,[Tdesc],TempID,productionID)values('{0}','','','','{1}','{2}')", uid, tempid, pid);
            return sql.ToString();
        }
        public string removeTemp(string uid, string pid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"delete from  T_Appuser_Temperature where tauloginID='{0}' and ProductionID='{1}'", uid, pid);
            return sql.ToString();
        }
        #endregion
    }
}